package com.ljx.controller;

import com.ljx.common.ResponseResult;
import com.ljx.domain.entity.Menu;
import com.ljx.domain.vo.MenuTreeVo;
import com.ljx.service.MenuService;
import com.ljx.service.RoleService;
import com.ljx.utils.SystemConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李捷禧
 * Date: 2023/4/22
 * ClassName: MenuController
 */

@RestController
@RequestMapping("system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult list(String menuName,String status){
        return menuService.listAll(menuName,status);
    }

    @PostMapping
    public ResponseResult add(@RequestBody Menu menu){
        menuService.save(menu);
        return ResponseResult.okResult("新增成功");
    }

    @GetMapping("/{id}")
    public ResponseResult getOne(@PathVariable Long id){
        Menu menu = menuService.getById(id);
        return ResponseResult.okResult(menu);
    }

    @PutMapping
    public ResponseResult update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return ResponseResult.okResult("修改成功");
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable List<Long> id){
        menuService.removeByIds(id);
        return ResponseResult.okResult("删除成功");
    }

    @GetMapping("/treeselect")
    public ResponseResult treeSelect(){
        List<Menu> list = menuService.list();
        List<MenuTreeVo> menuTreeVos = SystemConverter.buildMenuSelectTree(list);
        return ResponseResult.okResult(menuTreeVos);
    }

    @GetMapping("roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeSelect(@PathVariable Long id){
        return menuService.roleMenuTreeSelect(id);
    }

}
